package com.sdwl.brcl.validation.validation;

import java.util.Collection;

/**
 * @author xied
 * @date 2021年06月11日 14:12
 */
public class NotEmptyValidator implements Validator {
    public NotEmptyValidator() {
    }

    @Override
    public String validateCode() {
        return "not.empty";
    }

    @Override
    public String validateMsg(Object value, String validateName) {
        return validateName + "不能为空";
    }

    @Override
    public boolean validate(Object obj) {
        if (obj == null) {
            return false;
        } else if (obj instanceof Collection) {
            Collection<?> cts = (Collection)obj;
            return !cts.isEmpty();
        } else if (obj.getClass().isArray()) {
            return ((Object[])((Object[])obj)).length > 0;
        } else if (obj instanceof String) {
            return !((String)obj).isEmpty();
        } else {
            throw new UnsupportedOperationException("不支持的参数类型");
        }
    }
}
